#include "thread_pool.h"
#include "tlv_protocol.h"
#include "sql_commands.h"
#include "directory.h"
#define RESP_OK_MK 107
#define RESP_OK_RM 108
int delete_file(const char* filename) {
    // 检查输入参数是否有效
    if (filename == NULL || filename[0] == '\0') {
        fprintf(stderr, "错误：文件名不能为空\n");
        return -1;
    }

    // 尝试删除文件
    if (remove(filename) == 0) {
        printf("文件 '%s' 已成功删除\n", filename);
        return 0;
    } else {
        perror("删除文件失败");
        return -1;
    }
}
void*handler(void*arg){
    thread_pool_t *pthread_pool=(thread_pool_t*)arg;
    MYSQL *conn=db_connect("localhost", "root","123","ache");
    while(1){
        pthread_mutex_lock(&pthread_pool->mutex);
        while(pthread_pool->task_queue.size==0){
            pthread_cond_wait(&pthread_pool->cond,&pthread_pool->mutex);
        }
        int netfd=pthread_pool->task_queue.front->netfd;
        de_queue(&pthread_pool->task_queue);
        pthread_mutex_unlock(&pthread_pool->mutex);
        char arr[100]={0};
        int command =handle_command(netfd,arr);
        while(1){
            if(command==REGISTER){
                //注册用户
                printf("注册用户register\n");
                printf("arr=%s\n",arr);
                char temp_sql[1024]={0};
                snprintf(temp_sql,sizeof(temp_sql),"SELECT * FROM user WHERE user_name=='%s'",arr);
                int res = is_existed(conn,temp_sql);
                train_t train;
                if(res == -1){
                    //如果不存在 可以注册
                    //随机生成盐值
                    char salt_buf[30]={0};
                    generate_salt(salt_buf,30);
                    train.type =12;
                    train.length=strlen(salt_buf);
                    memcpy(train.value,salt_buf,strlen(salt_buf));
                    send(netfd,&train.type,4,MSG_NOSIGNAL);
                    send(netfd,&train.length,4,MSG_NOSIGNAL);
                    send(netfd,train.value,train.length,MSG_NOSIGNAL);
                    char encrypted_password[1024]={0};
                    int res = handle_command(netfd,encrypted_password);
                    if(res==13){
                        char temp_sql[4096]={0};
                        // 为每个变量分配足够空间存放转义后的值
                        char escaped_arr[1024] = {0};
                        char escaped_salt_buf[1024] = {0};
                        char escaped_encrypted_password[1024] = {0};

                        // 转义字符串
                        mysql_real_escape_string(conn, escaped_arr, arr, strlen(arr));
                        mysql_real_escape_string(conn, escaped_salt_buf, salt_buf, strlen(salt_buf));
                        mysql_real_escape_string(conn, escaped_encrypted_password, encrypted_password, strlen(encrypted_password));

                        // 使用转义后的值构建查询
                        snprintf(temp_sql, sizeof(temp_sql),
                                 "INSERT INTO USER(user_name, user_salt, encrypted_password) VALUES ('%s', '%s', '%s')",
                                 escaped_arr, escaped_salt_buf, escaped_encrypted_password);
                        int qret = mysql_query(conn,temp_sql);//把sql语句字符串发给服务端
                        if(qret != 0){
                            fprintf(stderr,"%s:%s\n","mysql_query",mysql_error(conn));
                            close(netfd);
                        }
                        
                    }

                }else{
                    train_t train;
                    train.type=11;
                    train.length=0;
                    send(netfd,&train.type,4,MSG_NOSIGNAL);
                    send(netfd,&train.length,4,MSG_NOSIGNAL);
                }
            }
            if(command==14){
                int res = logon(netfd,arr,conn);
                if(res==0){
                    printf("登录成功!用户是：%s\n",arr);
                    break;
                }
                else{
                    printf("登录失败\n");
                    close(netfd);
                }
            }
        }
    
    path_stack_t path_stack;
    init_stack(&path_stack);

    while(1){
        bzero(arr,sizeof(arr));
        int command=handle_command(netfd,arr);
        if(command==CD){
            //切换目录
            printf("切换目录cd\n");
            /* int res=handle_cd(conn,&path_stack,CPP68_PATH , arr); */
            /* train_t train; */
            /* if(res==0){ */
            /*     train.type=100; */
            /*     train.length=0; */
            /*     send(netfd,&train.type,4,MSG_NOSIGNAL); */
            /*     send(netfd,&train.length,4,MSG_NOSIGNAL); */
            /*     }else{ */
            /*         train.type=101; */
            /*         train.length=0; */
            /*         send(netfd,&train.type,sizeof(train.type),MSG_NOSIGNAL); */
            /*         send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL); */
            /*                        } */
            }
            else if(command==PWD){
                //展示当前工作目录
                printf("展示当前工作目录\n");
                /* handle_pwd(netfd, &path_stack); */
                
            }
            else if(command==LS){
                //展示当前目录
                printf("展示当前目录下所有文件\n");
                /* char arr[1024]={0}; */
                /* int res=handle_ls(conn,&path_stack,CPP68_PATH,arr,sizeof(arr)); */
                /* printf("res=%d\n",res); */
                /* train_t train; */
                /* if(res==0){ */
                /*     train.type=102; */
                /*     train.length=strlen(arr); */
                /*     memcpy(train.value,arr,strlen(arr)); */
                /*     send(netfd,&train.type,4,MSG_NOSIGNAL); */
                /*     send(netfd,&train.length,4,MSG_NOSIGNAL); */
                /*     send(netfd,train.value,train.length,MSG_NOSIGNAL); */
                /* }else{ */
                /*     train.type=101; */
                /*     train.length=0; */
                /*     send(netfd,&train.type,sizeof(train.type),MSG_NOSIGNAL); */
                /*     send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL); */
                /* } */
            }
            else if(command==GETS){
                //下载
                printf("下载\n");
                /* char real_path[256]={0}; */
                /* build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path)); */
                /* strcat(real_path, "/"); */
                /* strcat(real_path,arr); */
                /* printf("real_path=%s\n",real_path); */
                /* send_file_content(netfd, real_path); */
            }
            else if(command==PUTS){
                //上传
                printf("上传\n");
                /* char real_path[256]={0}; */
                /* build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path)); */

                /* char filename_to_create[512] = { 0 }; */
                /* snprintf(filename_to_create, sizeof(filename_to_create), "%s/%s", real_path, arr); */
                /* printf("filename_to_create=%s\n",filename_to_create); */

                /* receive_and_write_file(netfd, filename_to_create); */
            }
            else if(command==REMOVE){
                //删除文件
                printf("删除文件\n");
                /* char real_path[256]={0}; */
                /* build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path)); */
                /* strcat(real_path, "/"); */
                /* strcat(real_path,arr); */
                /* printf("real_path=%s\n",real_path); */
                /* int res=remove(real_path); */
                /* printf("res=%d\n",res); */
                /* train_t train; */
                /* if(res==0){ */
                /*     train.type=107; */
                /*     train.length=0; */
                /*     send(netfd,&train.type,4,MSG_NOSIGNAL); */
                /*     send(netfd,&train.length,4,MSG_NOSIGNAL); */
                /* }else{ */
                /*     train.type=108; */
                /*     train.length=0; */
                /*     send(netfd,&train.type,sizeof(train.type),MSG_NOSIGNAL); */
                /*     send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL); */


                /* } */
            }
            else if(command==MKDIR){
                //创建目录
                printf("创建目录\n");
                /* char real_path[256]={0}; */
                /* build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path)); */

                /* char filename_to_create[512] = { 0 }; */
                /* snprintf(filename_to_create, sizeof(filename_to_create), "%s/%s", real_path, arr); */
                /* printf("filename_to_create=%s\n",filename_to_create); */
                /* int res=create_directory_recursive(filename_to_create,0666); */
                /* printf("res=%d\n",res); */
                /* train_t train; */
                /* if(res==0){ */
                /*       train.type=107; */
                /*       train.length=0; */
                /*       send(netfd,&train.type,4,MSG_NOSIGNAL); */
                /*       send(netfd,&train.length,4,MSG_NOSIGNAL); */
                /*   }else{ */
                /*       train.type=108; */
                /*       train.length=0; */
                /*       send(netfd,&train.type,sizeof(train.type),MSG_NOSIGNAL); */
                /*       send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL); */

                /*   } */
            }
            else if(command==RMDIR){
                //删除目录
                printf("删除目录\n");
                /* char real_path[256]={0}; */
                /* build_real_path(&path_stack,CPP68_PATH,real_path,sizeof(real_path)); */
                /* strcat(real_path, "/"); */
                /* strcat(real_path,arr); */
                /* printf("real_path=%s\n",real_path); */
                /* int res=remove_directory(real_path); */
                /* printf("res=%d\n",res); */
                /* train_t train; */
                /* if(res==0){ */
                /*     train.type=107; */
                /*     train.length=0; */
                /*     send(netfd,&train.type,4,MSG_NOSIGNAL); */
                /*     send(netfd,&train.length,4,MSG_NOSIGNAL); */
                /* }else{ */
                /*     train.type=108; */
                /*     train.length=0; */
                /*     send(netfd,&train.type,sizeof(train.type),MSG_NOSIGNAL); */
                /*     send(netfd,&train.length,sizeof(train.length),MSG_NOSIGNAL); */


                /* } */
            }
            else if(command==QUIT){
                //退出
                printf("用户退出！\n");
                free_stack(&path_stack);
                close(netfd);
                break;
            }
            else{
                printf("无此命令!\n");
            }
            printf("command=%s\n",arr);

        }
    }
}
int make_worker(thread_pool_t*pthread_pool){
    for(int i=0;i<pthread_pool->tid_arr.worker_num;i++){
        pthread_create(&pthread_pool->tid_arr.arr[i],NULL,handler,pthread_pool);
    }
    return 0;
}

